package com.example.utils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

    /***
     * http请求并返回数据
     * @param host
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static byte[] getByBytes(String host, int connectTimeout, int readTimeout) {
        byte[] buffer = new byte[1024];
        HttpURLConnection connection = null;
        try {
            URL url = new URL(host);
            connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(readTimeout);
            connection.setConnectTimeout(connectTimeout);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept-Charset", "utf-8");
            connection.setRequestProperty("Charset", "UTF-8");
            if (200 == connection.getResponseCode()) {
                ByteArrayOutputStream outStream = null;
                try {
                    outStream = new ByteArrayOutputStream();
                    int len = 0;
                    while ((len = connection.getInputStream().read(buffer)) != -1) {
                        outStream.write(buffer, 0, len);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (outStream != null) {
                        outStream.close();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return buffer;
    }

    /***
     * 获取json的字符串
     * @param host
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static String get(String host, int connectTimeout, int readTimeout) {
        StringBuilder result = new StringBuilder(256);
        HttpURLConnection connection = null;
        try {
            URL url = new URL(host);
            connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(readTimeout);
            connection.setConnectTimeout(connectTimeout);
            connection.setRequestMethod("GET");
            connection.setRequestProperty("Accept-Charset", "utf-8");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Charset", "UTF-8");
            if (200 == connection.getResponseCode()) {
                BufferedReader reader = null;
                try {
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        result.append(line);
                    }
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return result.toString();
    }


    /***
     * 请求获取对象
     * @param host
     * @param connectTimeout
     * @param readTimeout
     * @return
     */
    public static Object getByObject(String host, int connectTimeout, int readTimeout) {
        Object object = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(host);
            connection = (HttpURLConnection) url.openConnection();
            connection.setReadTimeout(readTimeout);
            connection.setConnectTimeout(connectTimeout);
            connection.setRequestMethod("GET");
            if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) {
                ObjectInputStream inputStream = null;
                try {
                    inputStream = new ObjectInputStream(connection.getInputStream());
                    object = inputStream.readObject();
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return object;
    }
}
